package com.mhc.haval.controller;


import com.mhc.haval.api.dto.StaffDTO;
import com.mhc.haval.bo.EditPasswordBO;
import com.mhc.haval.bo.StaffBO;
import com.mhc.haval.constant.MsgCodeEnum;
import com.mhc.haval.exception.HavalCommonException;
import com.mhc.haval.form.ModifyPswForm;
import com.mhc.haval.form.StaffForm;
import com.mhc.haval.form.StaffListForm;
import com.mhc.haval.security.model.User;
import com.mhc.haval.service.StaffService;
import com.mhc.haval.vo.StaffInfoVO;
import com.mhc.haval.vo.StaffListVO;
import com.subaru.common.entity.BizResult;
import com.subaru.common.query.PageResult;
import com.subaru.common.util.BeanCopierUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HMJ on 2017/8/14.
 */
@Slf4j
@RestController
@RequestMapping(value = "/staff")     // 通过这里配置使下面的映射都在/staff下，可去除
public class StaffController {

    @Autowired
    private StaffService staffService;

    /**
     * 获取员工列表
     *
     * @param staffListForm 员工搜索框表单
     * @return
     */
    @ApiOperation(value = "获取员工列表", httpMethod = "GET", notes = "根据查询条件获取员工列表")
    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public BizResult getStaffList(StaffListForm staffListForm) {
        BizResult result = new BizResult();
        try {
            StaffBO staffBO = new StaffBO();
            BeanUtils.copyProperties(staffListForm, staffBO);
            PageResult<StaffBO> pageResult = staffService.queryByStaffList(staffBO);
            /**将BO转化成VO**/
            List<StaffListVO> staffList = new ArrayList<>();
            for (StaffBO staffbo : pageResult.getResult()) {
                StaffListVO staffListVO = new StaffListVO();
                BeanUtils.copyProperties(staffbo, staffListVO);
                staffList.add(staffListVO);
            }
            result.setData(staffList);
            result.setPageSize(staffBO.getPageSize());
            result.setPageNo(staffBO.getPageNo());
            result.setTotalCount(pageResult.getTotalCount());
            result.setCode(MsgCodeEnum.SUCCESS.getErrCode());
            result.setMessage(MsgCodeEnum.SUCCESS.getErrMsg());
            result.setSuccess(true);
            log.info("获取员工列表成功...");
            return result;
        } catch (HavalCommonException e) {
            log.error("getStaffList HavalCommonException",e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 新建员工
     *
     * @param staffForm
     * @return
     */
    @ApiOperation(value = "新建员工", httpMethod = "POST", notes = "创建新员工")
    @ApiImplicitParam(name = "staffForm", value = "员工详细实体", required = true, paramType = "body", dataType = "StaffForm")
    @RequestMapping(value = "/saveStaff", method = RequestMethod.POST)
    public BizResult saveStaff(@RequestBody StaffForm staffForm) {
        try {
            StaffBO staffBO = new StaffBO();
            BeanCopierUtil.copy(staffForm, staffBO);
            String[] roleArray = staffForm.getRoleArray();   //获取角色
            Boolean flag = staffService.addStaffInfo(staffBO, roleArray);
            log.info("新增员工成功...");
            return BizResult.create(flag, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
        } catch (HavalCommonException e) {
            log.error("saveStaff HavalCommonException",e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }

    /**
     * 修改员工
     *
     * @param staffForm
     * @return
     */
    @ApiOperation(value = "修改员工", httpMethod = "PUT", notes = "更新员工信息")
    @ApiImplicitParam(name = "staffForm", value = "员工详细实体staff", required = true, paramType = "body", dataType = "StaffForm")
    @RequestMapping(value = "/editStaff", method = RequestMethod.PUT)
    public BizResult editStaff(@RequestBody StaffForm staffForm) {
        try {
            StaffBO staffBO = new StaffBO();
            BeanCopierUtil.copy(staffForm, staffBO);
            String[] roleArray = staffForm.getRoleArray();   //获取角色
            Boolean updateFlag = staffService.editStaffInfo(staffBO, roleArray);
            log.info("修改员工成功...");
            return BizResult.create(updateFlag, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
        } catch (HavalCommonException e) {
            log.error("editStaff HavalCommonException",e);
            return BizResult.create(e.getCode(),e.getMessage());
        }

    }

    /**
     * 获取员工详细信息
     *
     * @param staffId
     * @return
     */
    @ApiOperation(value = "获取员工详细信息", notes = "根据url的id来获取员工详细信息")
    @ApiImplicitParam(name = "staffId", value = "员工ID", required = true, paramType = "path", dataType = "Long")
    @RequestMapping(value = "/look/{staffId}", method = RequestMethod.GET)
    public BizResult getStaffInfo(@PathVariable Long staffId) {
        try {
            StaffBO staffBO = staffService.queryStaffInfoByStaffId(staffId);
            log.info("获取员工详细信息成功...");
            return BizResult.create(BeanCopierUtil.convert(staffBO,StaffInfoVO.class), MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
        } catch (HavalCommonException e) {
            log.error("getStaffInfo HavalCommonException",e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 修改密码
     *
     * @param modifyPswForm
     * @return
     */
    @ApiOperation(value = "修改密码", notes = "")
    @ApiImplicitParam(name = "modifyPswForm", value = "修改密码实体", required = true, paramType = "body", dataType = "ModifyPswForm")
    @RequestMapping(value = "/editPassword", method = RequestMethod.POST)
    public BizResult editPassword(@RequestBody ModifyPswForm modifyPswForm,@AuthenticationPrincipal User logedUser) {
        try {
            EditPasswordBO editPasswordBO = new EditPasswordBO();
            BeanCopierUtil.copy(modifyPswForm, editPasswordBO);
            editPasswordBO.setStaffId(logedUser.getStaffId());
            Boolean editFlag = staffService.updateByPassword(editPasswordBO);
            log.info("修改密码成功...");
            return BizResult.create(editFlag, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
        } catch (HavalCommonException e) {
            log.error("editPassword HavalCommonException",e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 停用/启用员工
     *
     * @param staffForm 员工信息实体
     * @return
     */
    @ApiOperation(value = "停用/启用员工", httpMethod = "POST", notes = "")
    @ApiImplicitParam(name = "staffForm", value = "员工详细实体staff", required = true, paramType = "body", dataType = "StaffForm")
    @RequestMapping(value = "/startOrStopStaff", method = RequestMethod.POST)
    public BizResult startOrStopStaff(@RequestBody StaffForm staffForm) {
        try {
            StaffBO staffBO = new StaffBO();
            BeanCopierUtil.copy(staffForm, staffBO);
            Boolean editFlag = staffService.updateByStaffStatus(staffBO);
            if (editFlag) {
                log.info("修改员工状态成功...");
                return BizResult.create(null, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
            } else {
                log.info("修改员工状态失败...");
                return BizResult.create(MsgCodeEnum.UPDATE_FAIL.getErrCode(), MsgCodeEnum.UPDATE_FAIL.getErrMsg());
            }
        } catch (HavalCommonException e) {
            log.error("startOrStopStaff HavalCommonException",e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }


    /**
     * 重置密码
     *
     * @param staffId
     * @return
     */
    @ApiOperation(value = "重置密码", notes = "")
    @ApiImplicitParam(name = "staffId", value = "员工ID", required = true, paramType = "path", dataType = "Long")
    @RequestMapping(value = "/resetPassword/{staffId}", method = RequestMethod.GET)
    public BizResult resetPassword(@PathVariable Long staffId) {
        try {
            Boolean resetFlag = staffService.resetPassword(staffId);
            log.info("密码重置成功...");
            return BizResult.create(resetFlag, MsgCodeEnum.SUCCESS.getErrCode(), MsgCodeEnum.SUCCESS.getErrMsg());
        } catch (HavalCommonException e) {
            log.error("resetPassword HavalCommonException",e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }

}
